package com.yootk.servlet;

import com.yootk.common.util.DateConvert;
import com.yootk.servlet.handler.ExportHandler;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Date;

@WebServlet("/export.action")
public class ExportServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/xml;charset=UTF-8"); // XML显示风格
        Date beginDate = DateConvert.stringToDate(req.getParameter("begin")); // 开始日期时间
        Date endDate = DateConvert.stringToDate(req.getParameter("end")); // 结束日期事件
        resp.getOutputStream().print(new ExportHandler(beginDate, endDate).getData());
        String fileName = "yootk." + req.getParameter("begin") + "." + req.getParameter("end") + ".xml";
        resp.setHeader("Content-Disposition", "attachment; filename=" + fileName); // 设置文件后缀
    }
}
